package cn.wolfcode.rbac.service.impl;

import cn.wolfcode.rbac.domain.Department;
import cn.wolfcode.rbac.mapper.DepartmentMapper;
import cn.wolfcode.rbac.query.QueryObject;
import cn.wolfcode.rbac.service.IDepartmentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Service
@Transactional
public class DepartmentServiceImpl implements IDepartmentService {
   @Autowired
    private DepartmentMapper departmentMapper;
    @Override
    @Transactional(readOnly = true)
    public PageInfo query(QueryObject paramQueryObject) {
       /*Integer rows= Integer.valueOf(this.departmentMapper.selectForCount(paramQueryObject));
       if(rows.intValue()==0){
           return new PageResult(Integer.valueOf(paramQueryObject.getPageSize()));
       }
        List<Department> list = this.departmentMapper.selectForList(paramQueryObject);
        return new PageResult(Integer.valueOf(paramQueryObject.getCurrentPage()),rows,list,Integer.valueOf(paramQueryObject.getPageSize())); */
        PageHelper.startPage(paramQueryObject.getCurrentPage(), paramQueryObject.getPageSize());
        List<Department> list = this.departmentMapper.selectForList(paramQueryObject);
        return new PageInfo(list);
    }

    @Override
    public void saveOrUpdate(Department paramDepartment) {
        if (paramDepartment.getId()==null){
            this.departmentMapper.insert(paramDepartment);
        }else{
            this.departmentMapper.updateByPrimaryKey(paramDepartment);
        }
    }

    @Override
    public void delete(Long paramLong) {
        this.departmentMapper.deleteByPrimaryKey(paramLong);
    }

    @Override
    @Transactional(readOnly = true)
    public Department get(Long paramLong) {

        return this.departmentMapper.selectByPrimaryKey(paramLong);
    }

    @Override
    @Transactional(readOnly = true)
    public List<Department> list() {
        List<Department> departments = this.departmentMapper.selectAll();
        return departments;
    }
}
